1
2
3
4
5
6
7
8
9 package ca.uhn.cache.impl;
10
11 import java.text.ParseException;
12 import java.util.Calendar;
13 import java.util.Date;
14
15 import junit.framework.Test;
16 import junit.framework.TestCase;
17 import junit.framework.TestSuite;
18 import ca.uhn.cache.IDimension;
19
20
21 /***
22 * TODO complete javadoc for
23 *
24 * @author <a href="mailto:alexei.guevara@uhn.on.ca">Alexei Guevara </a>
25 * @version $Revision: 1.1 $ updated on $Date: 2005/01/24 22:51:48 $ by $Author:
26 * btripp $
27 */
28 public class DateRangeParamTest extends TestCase {
29
30
31 static {
32 ClassLoader.getSystemClassLoader().setPackageAssertionStatus( "ca.uhn.cache", true );
33 }
34
35 private IDimension myDimension1;
36 private IDimension myDimension2;
37 private IDimension myDimension3;
38
39 /***
40 * @return The test suite.
41 */
42 public static final Test suite() {
43 TestSuite retVal = new TestSuite( DateRangeParamTest.class );
44
45
46
47
48
49 return retVal;
50 }
51
52 /***
53 * @param theName The test name.
54 */
55 public DateRangeParamTest( String theName ) {
56 super( theName );
57 }
58
59 /***
60 * @see junit.framework.TestCase#setUp()
61 */
62 protected void setUp() throws Exception {
63 super.setUp();
64 myDimension1 = new Dimension( "dimension1", new Class[] { DateParam.class, DateRangeParam.class } );
65 myDimension2 = new Dimension( "dimension2", new Class[] { DateParam.class, DateRangeParam.class } );
66 myDimension3 = new Dimension( "dimension3", new Class[] { DateParam.class, DateRangeParam.class });
67 }
68
69 /***
70 * @see junit.framework.TestCase#tearDown()
71 */
72 protected void tearDown() throws Exception {
73 super.tearDown();
74 }
75
76 /***
77 */
78 public void test1() {
79 Date drStart = new Date();
80 Date drEnd = new Date();
81
82 try {
83 DateRangeParam drParam = new DateRangeParam( null, drStart, drEnd );
84 fail( "AssertionError must be thrown" );
85 }
86 catch (AssertionError e) {
87 }
88 }
89
90 /***
91 */
92 public void test2() {
93 Date drStart = new Date();
94 Date drEnd = new Date();
95
96 DateRangeParam drParam = new DateRangeParam( myDimension1, drStart, drEnd );
97
98 assertEquals( myDimension1, drParam.getDimension() );
99 assertEquals( drStart, drParam.getStart() );
100 assertEquals( drEnd, drParam.getEnd() );
101
102 }
103
104 /***
105 */
106 public void test3() {
107 Calendar calender = Calendar.getInstance();
108
109 Date drStart = calender.getTime();
110
111 calender.add( Calendar.YEAR, -1 );
112 Date drEnd = calender.getTime();
113
114 try {
115 DateRangeParam drParam = new DateRangeParam( myDimension1, drStart, drEnd );
116 fail( "AssertionError must be thrown!" );
117 }
118 catch (AssertionError e) {
119 }
120
121 }
122
123 /***
124 */
125 public void test4() {
126 Calendar calender = Calendar.getInstance();
127
128 Date drStart1 = calender.getTime();
129 Date drEnd1 = drStart1;
130 DateRangeParam drp1 = new DateRangeParam( myDimension1, drStart1, drEnd1 );
131
132 calender.add( Calendar.YEAR, +1 );
133
134 Date drStart2 = calender.getTime();
135 Date drEnd2 = drStart2;
136 DateRangeParam drp2 = new DateRangeParam( myDimension1, drStart2, drEnd2 );
137
138 assertFalse( drp1.intersects(drp2) );
139 assertFalse( drp2.intersects(drp1) );
140
141 }
142
143 /***
144 */
145 public void test5() {
146 Calendar calender = Calendar.getInstance();
147
148 Date drStart1 = calender.getTime();
149 Date drEnd1 = drStart1;
150 DateRangeParam drp1 = new DateRangeParam( myDimension1, drStart1, drEnd1 );
151
152 calender.add( Calendar.YEAR, -1 );
153
154 Date drStart2 = calender.getTime();
155 Date drEnd2 = drStart2;
156 DateRangeParam drp2 = new DateRangeParam( myDimension1, drStart2, drEnd2 );
157
158 assertFalse( drp1.intersects(drp2) );
159 assertFalse( drp2.intersects(drp1) );
160
161 }
162
163 /***
164 */
165 public void test6() {
166 Calendar calender = Calendar.getInstance();
167
168 Date drStart1 = calender.getTime();
169 Date drEnd1 = drStart1;
170 DateRangeParam drp1 = new DateRangeParam( myDimension1, drStart1, drEnd1 );
171
172 Date drStart2 = calender.getTime();
173 Date drEnd2 = drStart2;
174 DateRangeParam drp2 = new DateRangeParam( myDimension1, drStart2, drEnd2 );
175
176 assertTrue( drp1.intersects(drp2) );
177 assertTrue( drp2.intersects(drp1) );
178
179 }
180
181 /***
182 */
183 public void test7() {
184 Calendar calender1 = Calendar.getInstance();
185
186 Date drStart1 = calender1.getTime();
187
188 calender1.add( Calendar.YEAR, 1);
189 Date drEnd1 = calender1.getTime();
190
191 DateRangeParam drp1 = new DateRangeParam( myDimension1, drStart1, drEnd1 );
192
193 Calendar calender2 = Calendar.getInstance();
194
195 calender2.add( Calendar.DATE, 1 );
196 Date drStart2 = calender2.getTime();
197
198 calender2.add( Calendar.MONTH, 8 );
199 Date drEnd2 = calender2.getTime();
200
201 DateRangeParam drp2 = new DateRangeParam( myDimension1, drStart2, drEnd2 );
202
203 assertTrue( drp1.intersects(drp2) );
204
205 }
206
207 /***
208 */
209 public void test8() {
210 Calendar calender1 = Calendar.getInstance();
211
212 Date drStart1 = calender1.getTime();
213
214 calender1.add( Calendar.YEAR, 1);
215 Date drEnd1 = calender1.getTime();
216
217 DateRangeParam drp1 = new DateRangeParam( myDimension1, drStart1, drEnd1 );
218
219 Calendar calender2 = Calendar.getInstance();
220
221 calender2.add( Calendar.MONTH, 5 );
222 Date drStart2 = calender2.getTime();
223
224 calender2.add( Calendar.YEAR, 2 );
225 Date drEnd2 = calender2.getTime();
226
227 DateRangeParam drp2 = new DateRangeParam( myDimension1, drStart2, drEnd2 );
228
229 assertTrue( drp1.intersects(drp2) );
230 assertTrue( drp2.intersects(drp1) );
231
232 }
233
234 /***
235 */
236 public void test9() {
237 Calendar calender1 = Calendar.getInstance();
238
239 Date drStart1 = calender1.getTime();
240
241 calender1.add( Calendar.YEAR, 1);
242 Date drEnd1 = calender1.getTime();
243
244 DateRangeParam drp1 = new DateRangeParam( myDimension1, drStart1, drEnd1 );
245
246 Calendar calender2 = Calendar.getInstance();
247
248 calender2.add( Calendar.YEAR, -2 );
249 Date drStart2 = calender2.getTime();
250
251 calender2.add( Calendar.YEAR, 2 );
252 calender2.add( Calendar.MONTH, 2 );
253 Date drEnd2 = calender2.getTime();
254
255 DateRangeParam drp2 = new DateRangeParam( myDimension1, drStart2, drEnd2 );
256
257 assertTrue( drp1.intersects(drp2) );
258 assertTrue( drp2.intersects(drp1) );
259
260 }
261
262 /***
263 */
264 public void test10() {
265 Calendar calender = Calendar.getInstance();
266
267 Date drStart1 = calender.getTime();
268 Date drEnd1 = drStart1;
269 DateRangeParam drp1 = new DateRangeParam( myDimension1, drStart1, drEnd1 );
270
271 Date drStart2 = calender.getTime();
272 Date drEnd2 = drStart2;
273 DateRangeParam drp2 = new DateRangeParam( myDimension2, drStart2, drEnd2 );
274
275 assertFalse( drp1.intersects(drp2) );
276 assertFalse( drp2.intersects(drp1) );
277
278 }
279
280 /***
281 */
282 public void testPointIntersect() {
283 Date a = new Date(1000);
284 Date b = new Date(2000);
285 Date c = new Date(3000);
286
287 DateParam point = new DateParam(myDimension3, a);
288 DateRangeParam range = new DateRangeParam(myDimension3, b, c);
289 assertFalse(range.intersects(point));
290
291 point = new DateParam(myDimension3, b);
292 range = new DateRangeParam(myDimension3, a, c);
293 assertTrue(range.intersects(point));
294 }
295
296 /***
297 */
298 public void testPointMerge() {
299 Date a = new Date(1000);
300 Date b = new Date(2000);
301 Date c = new Date(3000);
302
303
304 DateParam dp = new DateParam(myDimension3, a);
305 DateRangeParam dr = new DateRangeParam(myDimension3, b, c);
306 DateRangeParam result = (DateRangeParam) dr.merge(dp);
307 assertEquals(a, ((DateRangeParam) result).getStart());
308 assertEquals(c, ((DateRangeParam) result).getEnd());
309
310
311 dp = new DateParam(myDimension3, c);
312 dr = new DateRangeParam(myDimension3, a, b);
313 result = (DateRangeParam) dr.merge(dp);
314 assertEquals(a, ((DateRangeParam) result).getStart());
315 assertEquals(c, ((DateRangeParam) result).getEnd());
316
317
318 dp = new DateParam(myDimension3, b);
319 dr = new DateRangeParam(myDimension3, a, c);
320 result = (DateRangeParam) dr.merge(dp);
321 assertEquals(a, ((DateRangeParam) result).getStart());
322 assertEquals(c, ((DateRangeParam) result).getEnd());
323 }
324
325 /***
326 */
327 public void testRangeMerge() {
328 Date a = new Date(1000);
329 Date b = new Date(2000);
330 Date c = new Date(3000);
331 Date d = new Date(4000);
332
333
334 DateRangeParam dr1 = new DateRangeParam(myDimension3, a, b);
335 DateRangeParam dr2 = new DateRangeParam(myDimension3, c, d);
336 assertEquals(dr1.merge(dr2), dr2.merge(dr1));
337
338
339 DateRangeParam result = (DateRangeParam) dr1.merge(dr2);
340 assertEquals(a, result.getStart());
341 assertEquals(d, result.getEnd());
342
343
344 dr1 = new DateRangeParam(myDimension3, a, c);
345 dr2 = new DateRangeParam(myDimension3, b, d);
346 result = (DateRangeParam) dr1.merge(dr2);
347 assertEquals(a, result.getStart());
348 assertEquals(d, result.getEnd());
349
350
351 dr1 = new DateRangeParam(myDimension3, a, d);
352 dr2 = new DateRangeParam(myDimension3, b, c);
353 result = (DateRangeParam) dr1.merge(dr2);
354 assertEquals(a, result.getStart());
355 assertEquals(d, result.getEnd());
356 }
357
358 /***
359 */
360 public void testPointDistance() {
361 DateRangeParam range = new DateRangeParam(myDimension3, new Date(1500), new Date(2500));
362 DateParam dp1 = new DateParam(myDimension3, new Date(1000));
363 DateParam dp2 = new DateParam(myDimension3, new Date(2000));
364 DateParam dp3 = new DateParam(myDimension3, new Date(3000));
365 DateParam sp = new DateParam(myDimension3, new Date(1500));
366
367
368 assertTrue(range.getDistance(dp1, sp) > 0);
369 assertTrue(range.getDistance(dp2, sp) == 0);
370 assertTrue(range.getDistance(dp3, sp) > 0);
371 assertTrue(range.getDistance(dp1, sp) - range.getDistance(dp3, sp) < .0001);
372 }
373
374 /***
375 */
376 public void testRangeDistance() {
377 Date a = new Date(1000);
378 Date b = new Date(2000);
379 Date c = new Date(3000);
380 Date d = new Date(4000);
381 DateParam sp = new DateParam(myDimension3, new Date(1500));
382
383
384
385 DateRangeParam dr1 = new DateRangeParam(myDimension3, a, b);
386 DateRangeParam dr2 = new DateRangeParam(myDimension3, c, d);
387 assertTrue(dr1.getDistance(dr2, sp) - dr2.getDistance(dr1, sp) < .0001);
388
389
390 assertTrue(dr1.getDistance(dr2, sp) > 0);
391 assertTrue(dr1.getDistance(dr2, sp) < 1);
392
393
394 dr1 = new DateRangeParam(myDimension3, a, c);
395 dr2 = new DateRangeParam(myDimension3, b, d);
396 assertTrue(dr1.getDistance(dr2, sp) < 0.0001);
397
398
399 dr1 = new DateRangeParam(myDimension3, a, d);
400 dr2 = new DateRangeParam(myDimension3, b, c);
401 assertTrue(dr1.getDistance(dr2, sp) < 0.0001);
402 }
403
404 /***
405 * @throws ParseException
406 * ...
407 */
408 public void testGetInstance1() throws ParseException {
409 DateRangeParam drp1 =
410 DateRangeParam.getInstance( myDimension1, "Jan 21, 2001 12:20 am", "Feb 21, 2004 1:20 am" );
411
412 DateRangeParam drp2 =
413 DateRangeParam.getInstance( myDimension1, "Jan 21, 2003 7:20 am", "Feb 21, 2010 1:20 am" );
414
415 assertTrue( drp2.intersects(drp1) );
416 }
417
418 /***
419 * @throws ParseException
420 * ...
421 */
422 public void testGetInstance2() throws ParseException {
423 DateRangeParam drp1 =
424 DateRangeParam.getInstance( myDimension1, "Jan 21, 2001 00:00 am", "Jan 21, 2001 00:00 pm" );
425
426 DateRangeParam drp2 =
427 DateRangeParam.getInstance( myDimension1, "Jan 21, 2000 7:20 am", "Feb 21, 2010 1:20 am" );
428
429 assertTrue( drp2.intersects(drp1) );
430 }
431
432
433 }
434